package com.etone.autotesting.support;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Logger;

import javax.ejb.Singleton;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;

import org.dom4j.Node;

import com.etone.app.support.decode.client.DataQueryServ;
import com.etone.ejb.core.util.PropertiesUtil;
import com.etone.ejb.core.util.XMLReadUtils;

@Singleton
public class PService extends Service {

	private static String WS_BINDPORT = "";

	private static String WS_NAMESPACE = "";
	private static URL DATAQUERYSERV_WSDL_LOCATION = null;
	private static String WS_ADDRESS = "";
	private final static Logger logger = Logger.getLogger(DataQueryServ.class.getName());

	static {
		GetUrl();
	}

	private static void GetUrl() {
		PropertiesUtil pro = PropertiesUtil.getInstance();
		WS_ADDRESS = pro.getProperty(Env.SERVICE_URL_ENV);
	}

	/**
	 * 
	 * @param <T>
	 * @param serviceClass
	 * @return
	 */
	private static <T> URL initUrl(Class<T> serviceClass,String serviceInfo) {
		URL url = null;
		try {
			URL baseUrl;
			baseUrl = serviceClass.getResource(".");
			StringBuilder builder = new StringBuilder(serviceClass.getSimpleName());
			builder = builder.deleteCharAt(0);
			String serviceAddress = WS_ADDRESS + builder.toString().toLowerCase() + "?wsdl";
			url = new URL(baseUrl, serviceAddress);
			String[] serivceInfos = serviceInfo.split("\\,");
			WS_NAMESPACE = serivceInfos[0];
			WS_BINDPORT = serivceInfos[2];
		} catch (MalformedURLException e) {
			logger.warning("Failed to create URL for the wsdl Location: " + WS_ADDRESS + ", retrying as a local file");
			logger.warning(e.getMessage());
			return url;
		}
		//String context = "";
//			InputStream stream = (InputStream) url.getContent();
//			XMLReadUtils xmlReader = new XMLReadUtils(stream);
//			Node wsdlNode = xmlReader.getSingleNode("//wsdl:definitions");
//			WS_NAMESPACE = xmlReader.getAttributeValue(wsdlNode, "targetNamespace");
//			Node wsdlServicePortNode = xmlReader.getSingleNode("//wsdl:definitions/wsdl:service/wsdl:port");
//			WS_BINDPORT = xmlReader.getAttributeValue(wsdlServicePortNode, "name");
			
		return url;
	}

	/**
	 * 
	 * @param <T>
	 * @param serviceClass
	 * @throws MalformedURLException
	 */
	public <T> PService(Class<T> serviceClass,String serviceInfo) throws MalformedURLException {
		super(initUrl(serviceClass,serviceInfo), new QName(WS_NAMESPACE, "ws"));
	}

	/**
	 * 创建Web客户端代理
	 * 
	 * @param wsdlLocation
	 *            WSDL地址
	 * @param targetNamespace
	 *            命名空间
	 * @param name
	 *            服务名称
	 * @param port
	 *            服务接口名称
	 * @throws MalformedURLException
	 */
	public PService(String wsdlLocation, String targetNamespace, String name, String port) throws MalformedURLException {
		super(new URL(wsdlLocation), new QName(targetNamespace, name));
		this.WS_BINDPORT = port;
		this.WS_NAMESPACE = targetNamespace;
	}

	/**
	 * 获得服务访问接口
	 * 
	 * @param serviceClass
	 *            服务接口
	 * @param <T>
	 *            服务接口类型
	 * @return
	 */
	public <T> T getService(Class<T> serviceClass) {
		return super.getPort(new QName(this.WS_NAMESPACE, this.WS_BINDPORT), serviceClass);
	}
}